package com.soft.dao;

import com.soft.entity.Goods;
import com.soft.util.Connect;

import java.util.ArrayList;
import java.util.List;

public class GoodsDao {

    Class<Goods> goodsClass;

    public GoodsDao(){
        goodsClass = (Class<Goods>) Goods.class;
    }

    public Goods get(int id) {
        List<Object> param = new ArrayList<Object>();
        param.add(id);
        String sql = "select * from goods where id=?";
        return Connect.getInstance().queryObject(sql, param, goodsClass);
    }

    public List<Goods> getAll() {
        List<Object> param = new ArrayList<Object>();
        List<Goods> list = new ArrayList<Goods>();
        String sql = "select * from goods";
        list = Connect.getInstance().queryListObject(sql, param, goodsClass);
        return list;
    }

    public List<Goods> getByType(int type) {
        List<Object> param = new ArrayList<Object>();
        param.add(type);
        List<Goods> list;
        String sql = "select * from goods where type=?";
        list = Connect.getInstance().queryListObject(sql, param, goodsClass);
        return list;
    }
    public boolean deleteGoods(int id){
        List<Object> param = new ArrayList<Object>();
        param.add(id);
        int flag;
        String sql="delete from goods where id=?";
        flag=Connect.getInstance().execute(sql,param);
        if(flag!=0){
            return true;
        }
        return false;

    }

    public boolean insert(Goods goods) {
        List<Object> param = new ArrayList<>();
        param.add(goods.getName());
        param.add(goods.getPrice());
        param.add(goods.getCreateTime());
        param.add(goods.getCount());
        param.add(goods.getType());
        param.add(goods.getDetail());

        String sql = "insert into goods (name, price, createtime, count, type, detail) value (?,?,?,?,?,?)";
        int flag = Connect.getInstance().execute(sql, param);
        if (flag != 0)
            return true;
        return false;
    }

    public boolean update(Goods goods) {
        List<Object> param = new ArrayList<Object>();
        param.add(goods.getName());
        param.add(goods.getPrice());
        param.add(goods.getCreateTime());
        param.add(goods.getCount());
        param.add(goods.getType());
        param.add(goods.getDetail());
        param.add(goods.getId());

        String sql = "update goods set name=?, price=?, createtime=?,count=?, type=?, detail=? where id=?";
        int flag = Connect.getInstance().execute(sql, param);
        if (flag != 0) {
            return true;
        }
        return  false;
    }
}
